package com.wjh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjh.enums.AppHttpCodeEnum;
import com.wjh.exception.SystemException;
import com.wjh.mapper.FlowerpotCompanyMapper;
import com.wjh.pojo.Florist;
import com.wjh.pojo.FlowerpotCompany;
import com.wjh.service.FlowerpotCompanyService;
import com.wjh.service.FlowerpotService;
import com.wjh.service.FlowerpotTypeService;
import com.wjh.utils.ResponseResult;
import com.wjh.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 花盆公司(FlowerpotCompany)表服务实现类
 *
 * @author makejava
 * @since 2023-10-17 19:37:54
 */
@Service("flowerpotCompanyService")
public class FlowerpotCompanyServiceImpl extends ServiceImpl<FlowerpotCompanyMapper, FlowerpotCompany> implements FlowerpotCompanyService {

    @Resource
    private FlowerpotTypeService flowerpotTypeService;

    @Resource
    private FlowerpotService flowerpotService;

    @Override
    public ResponseResult queryCompanyList(Integer pageNum, Integer pageSize, String name) {
        LambdaQueryWrapper<FlowerpotCompany> queryWrapper =
                new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,FlowerpotCompany::getName,name);
        //分页查询所有花店

        Page<FlowerpotCompany> certificatePage = new Page<>(pageNum,pageSize);
        Page<FlowerpotCompany> page = page(certificatePage, queryWrapper);
        return ResponseResult.okResult(new PageVo(page.getRecords(),page.getTotal()));
    }

    @Override
    public ResponseResult flowerpotCompanyAdd(FlowerpotCompany flowerpotCompany) {
        if(!StringUtils.hasText(flowerpotCompany.getName())){
            throw new SystemException(AppHttpCodeEnum.NEED_NAME);
        }
        save(flowerpotCompany);
        return ResponseResult.okResult();
    }

    @Override
    public FlowerpotCompany getNameById(Integer type) {
        return getById(type);
    }

    @Override
    public ResponseResult delCompanyByIds(List<Integer> id) {

        flowerpotTypeService.delByCompanyId(id);

        LambdaQueryWrapper<FlowerpotCompany> queryWrapper =
                new LambdaQueryWrapper<>();
        queryWrapper.in(FlowerpotCompany::getId,id);
        remove(queryWrapper);
        return ResponseResult.okResult();
    }
}
